package com.a01_2021.thousandstimes.instance.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.general.service.SysUserService;
import com.a01_2021.thousandstimes.instance.entity.Report;
import com.a01_2021.thousandstimes.instance.mapper.ReportMapper;
import com.a01_2021.thousandstimes.instance.service.IReportAttachmentService;
import com.a01_2021.thousandstimes.instance.service.IReportService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements IReportService {

    @Resource
    ReportMapper reportMapper;

    @Resource
    IReportAttachmentService reportAttachmentService;

    @Resource
    SysUserService sysUserService;

    /**
     * 添加报告
     *
     * @param userId            用户id
     * @param reportDescription 报告描述
     * @param reportTheme       报告主题
     * @param reportContent     报告内容
     * @param attachment        附件集合
     * @return HashMap<String, Object> 返还报告，附件
     * @throws CustomException 当用户不存在时抛出该异常
     */
    @Override
    public HashMap<String, Object> addReport(long userId, String reportDescription, String reportTheme, String reportContent, ArrayList<String> attachment) throws CustomException {
        if (!sysUserService.isUser(userId)) {
            //若用户不存在
            throw new CustomException(CustomExceptionType.USER_NOT_FOUND_ERROR, "该用户不存在");
        }
        //构建并插入报告
        Report report = new Report(null, userId, reportDescription, reportTheme, reportContent, LocalDateTime.now());
        reportMapper.insert(report);
        //插入报告附件
        reportAttachmentService.addAttachment(report.getId(), attachment);
        //构建返还HashMap
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("report", report);
        resultMap.put("attachment", attachment);
        return resultMap;
    }

    /**
     * 通过用户id与报告id查询报告
     *
     * @param userId   用户id
     * @param reportId 报告id
     * @return 返还HashMap
     * @throws CustomException 当用户不存在，查询报告不属于该用户时抛出
     */
    public HashMap<String, Object> getReport(long userId, long reportId) throws CustomException {
        if (!sysUserService.isUser(userId)) {
            //若用户不存在
            throw new CustomException(CustomExceptionType.USER_NOT_FOUND_ERROR, "该用户不存在");
        }
        //查询对应报告与附件
        return this.getReportFromUser(userId, reportId);
    }

    /**
     * 通过用户id与报告id查询对应报告
     *
     * @param userId   用户id
     * @param reportId 报告id
     * @return HashMap<String, Object>包含报告与附件
     * @throws CustomException 当用户不存在时抛出
     */
    public HashMap<String, Object> getReportFromUser(long userId, long reportId) throws CustomException {
        //查询对应报告
        Report report = reportMapper.selectById(reportId);
        if (report == null) {
            //报告不存在时
            throw new CustomException(CustomExceptionType.REPORT_NOT_FOUND_ERROR, "找不到该报告");
        }
        if (report.getId() != reportId) {
            //报告与用户id不符合
            throw new CustomException(CustomExceptionType.OPERATION_NOT_ALLOW, "查询用户id与报告不符");
        }
        //获取全部附件内容
        List<String> attachment = reportAttachmentService.getReportAttachment(reportId);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("report", report);
        resultMap.put("attachment", attachment);
        return resultMap;
    }

    /**
     * 通过用户id查询其报告
     *
     * @param userId 用户id
     * @return List报告列表（不包含报告内容
     */
    @Override
    public List<Report> getReports(long userId) throws CustomException {
        if (!sysUserService.isUser(userId)) {
            throw new CustomException(CustomExceptionType.USER_NOT_FOUND_ERROR, "找不到该用户");
        }
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        //此处不查询报告内容
        queryWrapper.select(Report.class, info -> !info.getColumn().equals("reportContent"));
        return reportMapper.selectList(queryWrapper);
    }

    /**
     * 通过用户id跟报告id修改报告内容
     *
     * @param userId            用户id
     * @param reportId          报告id
     * @param reportDescription 报告描述
     * @param reportTheme       报告主题
     * @param reportContent     报告内容
     * @return Report数据实体
     * @throws CustomException 当报告不存在，修改的报告不属于该用户时抛出
     */
    @Override
    public Report updateReport(long userId, long reportId, String reportDescription, String reportTheme, String reportContent) throws CustomException {
        Report report = reportMapper.selectById(reportId);
        if (report == null) {
            //报告不存在
            throw new CustomException(CustomExceptionType.REPORT_NOT_FOUND_ERROR, "找不到该报告");
        }
        if (report.getUserId() != userId) {
            //修改的报告不属于该用户
            throw new CustomException(CustomExceptionType.OPERATION_NOT_ALLOW, "不允许修改他人报告");
        }
        //将存在的修改数据替换
        if (!StringUtils.isEmpty(reportDescription)) {
            report.setReportDescription(reportDescription);
        }
        if (!StringUtils.isEmpty(reportTheme)) {
            report.setReportTheme(reportTheme);
        }
        if (!StringUtils.isEmpty(reportContent)) {
            report.setReportContent(reportContent);
        }
        report.setDate(LocalDateTime.now());//设置修改时间
        //同步到数据库
        reportMapper.updateById(report);
        return report;
    }
}
